package com.xiaoshuidi.cloud.module.rooms.api;

import cn.hutool.core.bean.BeanUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.tenant.core.context.TenantContextHolder;
import com.xiaoshuidi.cloud.module.rooms.api.home.vo.*;
import com.xiaoshuidi.cloud.module.rooms.api.home.HomeApi;
import com.xiaoshuidi.cloud.module.rooms.controller.app.home.vo.*;
import com.xiaoshuidi.cloud.module.rooms.service.HomeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.xiaoshuidi.cloud.module.system.enums.ApiConstants.VERSION;

@Slf4j
@RestController
@RequiredArgsConstructor
@DubboService(version = VERSION)
public class HomeApiImpl implements HomeApi {
    @Resource
    private HomeService homeService;

    /**
     * 集中式-搜索找房
     */
    @Override
    public CommonResult<PageResult<ConcentratedFindHouseRespApiVO>> concentratedFindHouse(String searchBox, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Integer sortRule, Boolean isWhole, Integer bedRoomNum, String publicFacilities, String lon, String lat, Integer pageNum, Integer pageSize) {
        log.info("[集中式-搜索找房Api接口被调用 ApartmentApi.concentratedFindHouse ]--tenantId:{}", TenantContextHolder.getTenantId());
        PageResult<ConcentratedFindHouseRespVO> checkedData = homeService.concentratedFindHouse(searchBox, district, monthRentalMin, monthRentalMax, longitude, latitude, sortRule, isWhole, bedRoomNum, publicFacilities, pageNum, pageSize, TenantContextHolder.getTenantId(), lon, lat).getCheckedData();
        List<ConcentratedFindHouseRespVO> list = checkedData.getList();
        List<ConcentratedFindHouseRespApiVO> respApiVOS = new ArrayList<>();
        for (ConcentratedFindHouseRespVO concentratedFindHouseRespVO : list) {
            ConcentratedFindHouseRespApiVO concentratedFindHouseRespApiVO = new ConcentratedFindHouseRespApiVO();
            BeanUtil.copyProperties(concentratedFindHouseRespVO, concentratedFindHouseRespApiVO);
            respApiVOS.add(concentratedFindHouseRespApiVO);
        }
        return CommonResult.success(new PageResult<>(respApiVOS, checkedData.getTotal()));
    }

    /**
     * 分散式-搜索找房
     */
    @Override
    public CommonResult<PageResult<NoConcentratedFindHouseRespApiVO>> noConcentratedFindHouse(String searchBox, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Integer sortRule, Boolean isWhole, Integer bedRoomNum, String publicFacilities, Long roomAreaId, Integer pageNum, Integer pageSize) {
        log.info("[分散式-搜索找房Api接口被调用 ApartmentApi.noConcentratedFindHouse ]--tenantId:{}", TenantContextHolder.getTenantId());
        PageResult<NoConcentratedFindHouseRespVO> checkedData = homeService.noConcentratedFindHouse(searchBox, district, monthRentalMin, monthRentalMax, longitude, latitude, sortRule, isWhole, bedRoomNum, publicFacilities, pageNum, pageSize, TenantContextHolder.getTenantId(), roomAreaId).getCheckedData();
        List<NoConcentratedFindHouseRespVO> list = checkedData.getList();
        List<NoConcentratedFindHouseRespApiVO> respApiVOS = new ArrayList<>();
        for (NoConcentratedFindHouseRespVO noConcentratedFindHouseRespVO : list) {
            NoConcentratedFindHouseRespApiVO noConcentratedFindHouseRespApiVO = new NoConcentratedFindHouseRespApiVO();
            BeanUtil.copyProperties(noConcentratedFindHouseRespVO, noConcentratedFindHouseRespApiVO);
            respApiVOS.add(noConcentratedFindHouseRespApiVO);
        }
        return CommonResult.success(new PageResult<>(respApiVOS, checkedData.getTotal()));
    }

    /**
     * 社区-门店详情
     * @param apartmentId
     * @return
     */
    @Override
    public CommonResult<ApartmentInfoRespApiVO> getApartmentInfo(Long apartmentId) {
        log.info("[获取门店详情Api接口被调用 ApartmentApi.getApartmentInfo ]--tenantId:{}", TenantContextHolder.getTenantId());
        GetApartmentInfoRespVO checkedData = homeService.getApartmentInfo(apartmentId).getCheckedData();
        ApartmentInfoRespApiVO apartmentInfoRespApiVO = new ApartmentInfoRespApiVO();
        BeanUtil.copyProperties(checkedData, apartmentInfoRespApiVO);
        return CommonResult.success(apartmentInfoRespApiVO);
    }

    @Override
    public CommonResult<RoomHouseInfoRespApiVO> getRoomHouseInfo(Long roomHouseId) {
        log.info("[获取房源信息Api接口被调用 ApartmentApi.getRoomHouseInfo ]--roomHouseId:{}", roomHouseId);
        GetRoomHouseInfoRespVO checkedData = homeService.getRoomHouseInfo(roomHouseId).getCheckedData();
        RoomHouseInfoRespApiVO roomHouseInfoRespApiVO = new RoomHouseInfoRespApiVO();
        BeanUtil.copyProperties(checkedData, roomHouseInfoRespApiVO);
        return CommonResult.success(roomHouseInfoRespApiVO);
    }

    /**
     * 房源详情-同房源其他房间
     * @param roomHouseId
     * @param tenantId
     * @return
     */
    @Override
    public CommonResult<List<OtherRoomsInTheSameListingRespApiVO>> otherRoomsInTheSameListing(Long roomHouseId, Long tenantId) {
        log.info("[获取同房源其他房间Api接口被调用 ApartmentApi.otherRoomsInTheSameListing ]--roomHouseId:{}, tenantId:{}", roomHouseId, tenantId);
        List<OtherRoomsInTheSameListingRespVO> checkedData = homeService.otherRoomsInTheSameListing(roomHouseId, tenantId).getCheckedData();
        List<OtherRoomsInTheSameListingRespApiVO> respVOS = new ArrayList<>();
        for (OtherRoomsInTheSameListingRespVO checkedDatum : checkedData) {
            OtherRoomsInTheSameListingRespApiVO respVO = new OtherRoomsInTheSameListingRespApiVO();
            BeanUtil.copyProperties(checkedDatum, respVO);
            respVOS.add(respVO);
        }
        return CommonResult.success(respVOS);
    }

    /**
     * 房源详情-推荐附近房源
     * @param roomHouseId
     * @param tenantId
     * @return
     */
    @Override
    public CommonResult<List<OtherRoomsInTheSameListingRespApiVO>> recommendListingsNearYou(Long roomHouseId, Long tenantId) {
        log.info("[获取推荐附近房源Api接口被调用 ApartmentApi.recommendListingsNearYou ]--roomHouseId:{}, tenantId:{}", roomHouseId, tenantId);
        List<OtherRoomsInTheSameListingRespVO> checkedData = homeService.recommendListingsNearYou(roomHouseId, tenantId).getCheckedData();
        List<OtherRoomsInTheSameListingRespApiVO> respVOS = new ArrayList<>();
        for (OtherRoomsInTheSameListingRespVO checkedDatum : checkedData) {
            OtherRoomsInTheSameListingRespApiVO respVO = new OtherRoomsInTheSameListingRespApiVO();
            BeanUtil.copyProperties(checkedDatum, respVO);
            respVOS.add(respVO);
        }
        return CommonResult.success(respVOS);
    }

    /**
     * 房型详情
     * @param apartmentId
     * @param layoutId
     * @return
     */
    @Override
    public CommonResult<GetLayoutInfoRespApiVO> getLayoutInfo(Long apartmentId, Long layoutId) {
        log.info("[获取房型详情Api接口被调用 ApartmentApi.recommendListingsNearYou ]--apartmentId:{}, layoutId:{}", apartmentId, layoutId);
        GetLayoutInfoRespVO checkedData = homeService.getLayoutInfo(apartmentId, layoutId).getCheckedData();
        GetLayoutInfoRespApiVO getLayoutInfoRespApiVO = new GetLayoutInfoRespApiVO();
        BeanUtil.copyProperties(checkedData, getLayoutInfoRespApiVO);
        return CommonResult.success(getLayoutInfoRespApiVO);
    }

    /**
     * 查询房源房型
     * @return
     */
    @Override
    public CommonResult<List<RoomLayoutVO>> queryRoomLayout() {
        return homeService.queryRoomLayout();
    }


}
